import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
const routes = [
  // 首页路由（新增懒加载）
  {
    path: '/',
    redirect: to => ({      
      path: '/wx-callback'
    })
  },
  {
    path: '/doctor/login',
    name: 'DoctorLogin',
    component: () => import('@/views/doctor/Login.vue')
  },
  // 医生端路由
  {
    path: '/doctor/reports',
    name: 'DoctorReportList',
    component: () => import('@/views/doctor/ReportList.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/doctor/detail/:id',
    name: 'DoctorReportDetail',
    component: () => import('@/views/doctor/ReportDetail.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/wx-callback',
    name: 'WxCallback',
    component: () => import('@/views/doctor/WxCallback.vue'),
    meta: { requiresAuth: true }
  },
  // 柯桥院区路由
  {
    path: '/keqiao/search',
    name: 'KeqiaoSearch',
    component: () => import('@/views/keqiao/Search.vue')
  },
  {
    path: '/keqiao/reports',
    name: 'KeqiaoReportList',
    component: () => import('@/views/keqiao/ReportList.vue')
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})
// 如果是测试环境不需要微信授权
if (import.meta.env.MODE !== 'development') {
  router.beforeEach((to, from, next) => {
    const authStore = useAuthStore();
    if (to.meta.requiresAuth && !authStore.wxUserInfo) {
      authStore.getWxAuth();
    } else {
      next();
    }
  });
}

export default router 